#! /bin/bash

# License:
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later
#   version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this program.  If not, see
#   <https://www.gnu.org/licenses/>.
#
## You can redistribute it as you wish : GPL v3
## author : wildtruc@noneltd.net

## TODO
# XXX reconfigurer le xml xrandr
# XXX créer un UI pour les fichiers de config.
# XXX indiquer le dir source des librairies detectées pour l'UI de config de la source des ficheirs.  (use 'find)')
# indiquer la précédence de monitor.xml généré par l'ui de xrandr.

vn='<span weight=\"normal\">'
vb='<span weight=\"bold\">'
if [[ -s /etc/nvidia-prime/options.conf ]]; then
	. /etc/nvidia-prime/options.conf
	if [ $font_style -gt 0 ]; then
	## clear background
		vn='<span color=\"#00A400\" weight=\"normal\">'
		vb='<span color=\"#00A400\" weight=\"bold\">'
	else
	## dark background
		vn='<span color=\"#005400\" weight=\"normal\">'
		vb='<span color=\"#005400\" weight=\"bold\">'
	fi
fi
end='</span>'
j='<span color=\"#FF6800\">'
rBB='<span color=\"#FF3300\" font=\"20\">'
## prime_setup
# first ui msg.
prime_msg_01="Select the Prime setup you would like to use:"
prime_msg_02="Default is currently set to $j%s$end."
menu_prime_01="Set Prime to Intel device"
menu_prime_02="Set Prime to Nvidia device"
menu_prime_03="Edit Libraries config file"
menu_prime_04="Edit current Xorg config file"
menu_prime_05="Edit options config file"
# ref
m_prime_01="intel"
m_prime_02="nvidia"
# last wrn msg
wrn_prime_01="Prime will be set to $j%s$end."
wrn_prime_02="\nYou may now restart the user session to take effect."
# nvidia-prime-select path and files
xorg_prime=/etc/nvidia-prime
lib_conf=/etc/nvidia-prime/library.conf
opt_conf=/etc/nvidia-prime/options.conf
## su cmd is currently manages by polkit
## setup color, grub, user's xrandr options.
edit_options(){
	edit_options_conf=$(zenity --width=500 --height=400 --title="options conf editor" --icon-name=nvidia-prime \
	--text-info --editable --filename="$opt_conf" --checkbox="Confirm to overwrite" \
	--text="$vb\\Edit nvidia-prime-select and GUI options.$end")
	if [ $? = 0 ]; then
		pkexec bash -c "echo -e \"$edit_options_conf\" > $opt_conf"
		notify-send -t 5000 -u low  "Options data saved."
	fi
	bash -c nvidia-prime-ui
	exit 0
}
edit_conf(){
	edit_lib_conf=$(zenity --width=500 --height=400 --title="libraries conf editor" --icon-name=nvidia-prime \
	--text-info --editable --filename="$lib_conf" --checkbox="Confirm to overwrite" \
	--text="$vb\\Edit libraries path to fit your system install.$end")
	if [ $? = 0 ]; then
		if [ $(printf "$edit_lib_conf"| sed -En "s/^(.*)='(.*)'$/\2/g;p"| grep -c .) -gt 0 ]; then
			pkexec bash -c "echo -e \"$edit_lib_conf\" > $lib_conf"
			notify-send -t 5000 -u low  "libraries data saved."
		fi
	fi
	bash -c nvidia-prime-ui
	exit 0
}
edit_xorg(){
	if [ $(file /etc/X11/xorg.conf | grep -c "$xorg_prime.*$current_set") -eq 0 ]; then
		xorg_file=$xorg_prime/$(file /etc/X11/xorg.conf | sed -n "s/^.*\///p")
	else
		xorg_file=$xorg_prime/xorg.$current_set.conf
	fi
	edit_xorg_conf=$(zenity --width=500 --height=400 --title="Xorg $current_set conf editor" \
	--icon-name=nvidia-prime --text-info --editable \
	--filename="$xorg_file" --checkbox="Confirm to overwrite" \
	--text="$vb\\Edit current xorg conf as appropriate to fit your needs.$end")
	if [ $? = 0 ]; then
		_xorg_conf=$(echo -e "$edit_xorg_conf"| sed -En 's|\"|\\"|g;p')
		pkexec bash -c "echo -e \"$_xorg_conf\" > $xorg_file"
		notify-send -t 5000 -u low  "Xorg conf file saved."
	fi
	bash -c nvidia-prime-ui
	exit 0
}
setup_prime(){
	if [[ -s $HOME/.config/monitors.xml ]]; then
		prev_conf="\n\nNotice$vn: Display looks already configured by$j xrandr$end session tool UI.\nUI settings take precedence over$j nvidia-prime-select$end auto set.\nBe aware it could create conflicts at session restart (blank screen).$end"
	fi
	xrandr_msg="$vb\n\nCheck your display settings before restart.$end"
	zenity --height=100 --title="nvidia-prime-select setup" --question --no-wrap \
	--icon-name=nvidia-prime --text="$vb$(printf "$wrn_prime_01$wrn_prime_02" "$_prime")$prev_conf$xrandr_msg$end" \
	--cancel-label='Cancel' --ok-label='Setup'
	if [ $? = 0 ]; then
		pkexec /usr/sbin/nvidia-prime-select $_pset
		notif_msg=$(printf "Session will restart with %s hardware.\nPlease restart your session." "$_pset")
	else
		bash -c nvidia-prime-ui
		exit 0
	fi
	
}
#root_id
unset setup_list setup_option _pset
#setup_option=(
#"$menu_prime_01;$m_prime_01;/etc/X11/xorg.conf"
#"$menu_prime_02;$m_prime_02;/etc/X11/xorg.conf"
#"$menu_prime_03;;"
#"$menu_prime_04;;"
#"$menu_prime_05;;"
#)
setup_option=(
"$menu_prime_01;$m_prime_01"
"$menu_prime_02;$m_prime_02"
"$menu_prime_03;"
"$menu_prime_04;"
"$menu_prime_05;"
)
n=1
for p_set in "${setup_option[@]}"; do
	pc_set=$(printf "$p_set"|cut -d';' -f1)
	pc_msg=$(printf "$p_set"|cut -d';' -f2)
#	pc_file=$(printf "$p_set"|cut -d';' -f3)
	setup_list+=("false" "$n" "$pc_set")
	if [[ $pc_msg != '' ]]; then
		if [ $(file -p /etc/X11/xorg.conf| grep -c "xorg.$pc_msg.conf") -gt 0 ]; then
			current_set=$pc_msg
		fi
	else
		current_set=$(cat /etc/X11/xorg.conf| grep -v '#'| sed -En "s/^.*Screen.*0.*\"(.*)_screen\".*$/\1/p")
	fi
	((n++))
done

if [[ "$current_set" ]]; then
	prime_msg="$prime_msg_01\n$(printf "$prime_msg_02" "$current_set")"
else
	prime_msg="$prime_msg_01\nNo device is currently set."
fi

menu_prime=$(zenity --width=340 --list --radiolist --hide-header \
--title="nvidia-prime-select" --text "$vb$prime_msg$end" \
--column "1" --column "2" --column "3" --separator=";" --hide-column=2 \
"${setup_list[@]}" --ok-label='Select' --cancel-label='Exit')
if [ $? = 1 ]; then exit 0; fi
case $menu_prime in
	"1") _pset="intel"; _prime="$m_prime_01" ;;
	"2") _pset="nvidia"; _prime="$m_prime_02" ;;
	"3") edit_conf ;;
	"4") edit_xorg ;;
	"5") edit_options ;;
esac
setup_prime
if [[ $notif_msg != '' ]]; then
	notify-send -t 5000 -u low --icon=nvidia-prime "Nvidia-prime setup applied." "$notif_msg."
fi
exit 0
